package Classes;

import Forms.*;
import java.io.File;
import java.rmi.*;
import java.rmi.server.*;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mau
 */
public class Server extends UnicastRemoteObject implements MInterface {
    Database db;
    ArrayList<User> usersList;
    DateTime dt;
    ServerFrame sf;
    int clients;
    int users;
    
    ArrayList<User> userEdit;
    ArrayList<Curriculum> currList;
    ArrayList<Curriculum> currListSearch;
    ArrayList<Event> event;
    ArrayList<ClassLevel> level;
    ArrayList<Subject> subject;
    ArrayList<Subject> subjectFilter;
    
    ArrayList<Object> list;
    ArrayList<Object> cblist;
    ArrayList<Object> searchList;
    ArrayList<Grades> gradesS;
    ArrayList<Grades> gradesSS;
    ArrayList<Grades> grades;
    
    ArrayList<Sections> secList;
        
    ArrayList<SchoolYear> schoolyear;
    
    public Server(ServerFrame a) throws RemoteException {
        sf = a;
        db = new Database(sf);
        dt = new DateTime();
        clients = sf.getNum("clients");
        users = sf.getNum("users");
    }
        
    @Override
    public User getUser(String username) throws RemoteException {
        return db.getUser(username);
    }
    
    @Override
    public String getCurrentTime() throws RemoteException {
        return dt.getCurrentTime();
    }
    
    @Override
    public String getCurrentDate() throws RemoteException {
        return dt.getCurrentDate();
    }
    
    @Override
    public String getDBDate() throws RemoteException {
        return dt.getDBDate();
    }
    
    @Override
    public String getCurrDate() throws RemoteException {
        return dt.getCurrDate();
    }
    
    @Override
    public String getCurrYear() throws RemoteException {
        return dt.getCurrYear();
    }
    
    @Override
    public boolean checkUserStatus(String username) throws RemoteException {
        try {
            boolean in = db.checkStatus(username);
            if(in) {
                return true;
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean login(String username, String password, boolean status) throws RemoteException {
        boolean loggedin = false;
        
        try {
            usersList = (ArrayList<User>) getList("getlist-users").clone();
            for(User u : usersList) {
                if(username.equals(u.getUsername()) && password.equals(u.getPassword()) && !status) {
                    loggedin = true;
                    db.changeUserStatus(username, "In");
                    users += 1;
                    sf.appendTextArea(username + " has logged in.");
                    sf.updateLabel("users", users);
                } else if(username.equals(u.getUsername()) && password.equals(u.getPassword())) {
                    loggedin = true;
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return loggedin;
    }
    
    @Override
    public void logout(String username) throws RemoteException {
        try {
            if(!username.equals("")) {
                db.changeUserStatus(username, "Out");
                users -= 1;
                sf.appendTextArea(username + " has logged out.");
                sf.updateLabel("users", users);
            } else {
                ArrayList<User> uuu = (ArrayList<User>) db.getList("getlist-users").clone();
                for(int x = 0; x < uuu.size(); x++) {
                    db.changeUserStatus(uuu.get(x).getUsername(), "Out");
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void clientActivity(String username, String activity) {
        if(activity.equals("connect")) {
            clients += 1;
            sf.appendTextArea(username + " : A client has connected to the server.");
            sf.updateLabel("clients", clients);
        }
        
        if(activity.equals("disconnect")) {
            clients -= 1;
            sf.appendTextArea(username + " : A client has disconnected from the server.");
            sf.updateLabel("clients", clients);
        }
    }
    
    public String userType(String username) throws Exception {
        return db.userType(username);
    }
    
    @Override
    public ArrayList<Object> getList(String table)throws Exception {
        list = (ArrayList<Object>)db.getList(table).clone();
        return list;
    }    
    
    public ArrayList<Object> getList2(String table, String a, String b)throws Exception {
        list = (ArrayList<Object>)db.getList2(table, a, b).clone();
        return list;
    }
    @Override
    public ArrayList<Object> getList4(String table, String a)throws Exception {
        list = (ArrayList<Object>)db.getList4(table, a).clone();
        return list;
    }  
    
    public Object get(String table, String a, String b)throws Exception {
        return db.get(table, a, b);
    }
    
    public boolean add(String table, Object a, String clvl, User aa)throws Exception {
        return db.add(table, a, clvl, aa);
    }    

    @Override
    public String getRealYear() throws Exception {
        return dt.getRealYear();
    }

    @Override
    public String getPrevYear() throws Exception {
        return dt.getPrevYear();
    }

    @Override
    public boolean getPassword(String username, String password) throws RemoteException {        
        boolean getpw = false;
        try {
            for(User u : usersList) {
                if(username.equals(u.getUsername()) && password.equals(u.getPassword())) {
                    getpw = true;                    
                    users += 1;                        
                } else if(username.equals(u.getUsername()) && password.equals(u.getPassword())) {
                    getpw = true;
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return getpw;
    
    }

    @Override
    public String getYear() throws RemoteException {
        return dt.getYear();
    }

    @Override
    public int getToday() throws Exception {
        return dt.getToday();
    }

    @Override
    public int getPast() throws Exception {
        return dt.getPast();
    }

    @Override
    public int getNext() throws Exception {
        return dt.getNext();
    }

    @Override
    public String addEvent(Event ee, User a) throws Exception {
        return db.addEvent(ee, a);
    }

    @Override
    public String getEndSy(String end) throws Exception {
        return db.getEndSy(end);
    }

    @Override
    public String getStartSy(String start) throws Exception {
        return db.getStartSy(start);
    }
    
     public ArrayList<Object> getList3(String table, String a, String b)throws Exception {
        list = (ArrayList<Object>)db.getList3(table, a, b).clone();
        return list;
    }  
      @Override
    public String addGrade(double a1, int a2, int a3, String a4, String a5) throws RemoteException {
        return db.addGrade(a1, a2, a3, a4, a5);
    }
      
       @Override
    public String editGrade(double a1, int a2, int a3, String a4, String a5) throws RemoteException {
        return db.editGrade(a1, a2, a3, a4, a5);
    }

    @Override
    public String addAttendance(String a, int a1, int a2, int a3, int a4, String a5) throws RemoteException {
        return db.addAttendance(a, a1, a2, a3, a4, a5);
    }

    @Override
    public String editAttendance(String a, int a1, int a2, int a3, int a4, String a5) throws RemoteException {
        return db.editAttendance(a, a1, a2, a3, a4, a5);
    }
    
    @Override
    public ArrayList<Object> SSList() throws Exception {
        return (ArrayList<Object>) db.SSList().clone();
    }

    @Override
    public ArrayList<Object> SSSList(String a) throws Exception {
        return (ArrayList<Object>) db.SSSList(a).clone();
    }

    @Override
    public Object SumSub(String a, String b) throws Exception {
        return db.SumSub(a, b);
    }

    @Override
    public String getSummerSave(Summer sum, String sy, ArrayList<Object> subList, User a) throws Exception {
        return db.getSummerSave(sum, sy, subList, a);
    }

    @Override
    public String editManage(String table, String Orig, Object o, User a) throws Exception {
        return db.editManage(table, Orig, o, a);
    }

    @Override
    public String SumSubjectAdd(ArrayList<Object> nSub, String sSY) throws Exception {
        return db.SumSubjectAdd(nSub, sSY);
    }

    @Override
    public ArrayList<Object> cbList(String t) throws Exception {
        return (ArrayList<Object>) db.cbList(t).clone();
    }

    @Override
    public boolean edit(String table, Object a, User aa) throws Exception {
        return db.edit(table, a, aa);
    }

    @Override
    public Event getEvent(String event) throws Exception {
        return db.getEvent(event);
    }

    @Override
    public SchoolYear getDays(String days) throws Exception {
        return db.getDays(days);
    }
    
     @Override
    public ArrayList<Object> SumList(String sy) throws Exception {
        return (ArrayList<Object>) db.SumList(sy).clone();
    }
    
    @Override
    public String SumSubAdd(String nSub) throws Exception {
        return db.SumSubAdd(nSub);
    }

    @Override
    public ArrayList<ClassLevel> listLevel() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
